<template>
	<view class="content">
		<cu-custom bgColor="bg-gradual-blue" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">添加部门</block>
		</cu-custom>

		<form id="formEmplotee" @submit="formSubmit">
			<view class="padding">
				{{title}}
			</view>
			<view class="cu-form-group">
				<view class="title">名称</view>
				<input placeholder="请输入部门名称" name="departname"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">电话</view>
				<input placeholder="请输入电话" maxlength="11" name="mobile"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">地址</view>
				<input placeholder="请输入地址" name="address"></input>
			</view>
			<view class="cu-form-group align-start margin-top">
				<view class="response">
					<view class="title">部门描述</view>
					<textarea name="description" maxlength="-1" placeholder="请输入部门描述"></textarea>
				</view>
			</view>

			<view class="padding">
				<button style="background-color: #5599FF;" type="primary" form-type="submit">添加</button>
			</view>
		</form>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: '未获取到上级部门，请返回上级重试',
				parent: {},
				depth: 0
			}
		},
		onLoad(options) {
			console.log(options)
			if (typeof(options.parent) != 'undefined') {
				let parent = JSON.parse(options.parent)
				this.parent = parent
				this.depth = options.depth
				this.title = '上级部门:' + parent.deptName
			}
		},
		methods: {
			formSubmit(e) {
				let form = e.detail.value
				console.log(form)
				if (form.departname != "") {
					this.goAdd(form)
				} else {
					uni.showToast({
						title: '请输入部门名',
						icon: 'none'
					})
				}
			},
			goAdd(form) {
				//说明已有数据，目前处于上拉加载
				uni.request({
					url: this.$constant.BASEURL + this.$constant.ADD_DEPARTMENT,
					method: "POST",
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						pid: this.parent.id,
						orgType: this.depth > 1 ? 2 : 1,
						departname: form.departname,
						description: form.description,
						mobile: form.mobile,
						address: form.address
					},
					success: (res) => {
						// console.log(res.data)
						if (res.data.code == 0) {
							uni.showToast({
								title: '添加成功',
								icon: 'success'
							})
							let pages = getCurrentPages()
							uni.navigateBack({
								delta: pages.length - 2
							})
						} else {
							uni.showToast({
								title: res.data.message,
								icon: 'none'
							})
						}
					}
				});

			}
		}
	}
</script>

<style>

</style>
